/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.supermercadobarateiro.bd;

import br.edu.ifal.supermercadobarateiro.modelo.Fornecedor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mikaely Anaeve
 */
public class BdFornecedor {
   

    public void insere(Fornecedor f) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();

        String sql = "insert into cadastrofornecedor "
                + "(nome,cidade,email,senha,fornecedor,endereco,bairro) "
                + "values (?,?,?,?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, f.getNome());
        
        stmt.setString(2, f.getCidade());
        stmt.setString(3, f.getEmail());
        
        // stmt.setString(7, f.getSexo());
        
       stmt.setString(4, f.getSenha());
       stmt.setString(5, f.getFornecedor());
       stmt.setString(6, f.getEndereco());
        stmt.setString(7, f.getBairro());
//        stmt.setDate(10, new java.sql.Date(f.getDatadeNascimento().getTimeInMillis()));
        stmt.execute();
        stmt.close();
        con.close();
    }

    public void altera(Fornecedor f) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "update cadastrofornecedor set nome=?,senha=?,fornecedor=?,cidade=?,email=?,endereco=?,bairro=?"
                + " where cod=?";

        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, f.getNome());
        stmt.setString(2, f.getSenha());
        stmt.setString(3, f.getFornecedor());
        stmt.setString(4, f.getCidade());
        stmt.setString(5, f.getEmail());
       stmt.setString(6, f.getEndereco());
       stmt.setString(7, f.getBairro());
        stmt.setInt( 8, f.getCod());
        //stmt.setDate(10, new java.sql.Date(f.getDatadeNascimento().getTimeInMillis()));

        stmt.execute();
        stmt.close();
        con.close();
    }

    public void remove(Fornecedor f) throws SQLException {

        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = " delete from cadastrofornecedor where cod = ?";
        PreparedStatement stmt = con.prepareStatement("delete "
                + "from cadastrofornecedor where cod=?");
        stmt.setInt(1, f.getCod());
        stmt.execute();
        stmt.close();



    }

    public Fornecedor busca(int cod) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "select *from cadastrofornecedor where cod = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1,cod);
        ResultSet rs = stmt.executeQuery();
        Fornecedor f1 = new Fornecedor();
        while (rs.next()) {

            f1.setNome(rs.getString("nome"));
            f1.setEmail(rs.getString("email"));
             f1.setCidade(rs.getString("cidade"));
           
                f1.setSenha(rs.getString("senha"));
                f1.setFornecedor(rs.getString("fornecedor"));
                f1.setEndereco(rs.getString("endereco"));
            f1.setBairro(rs.getString("bairro"));


        }
        rs.close();
        stmt.close();
        con.close();
        return f1;
    
}
    }